import secrets
from typing import Any, Annotated
from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic import AnyUrl, BeforeValidator, computed_field

def parse_cors(v: Any) -> list[str] | str:
    if isinstance(v, str) and not v.startswith("["):
        return [i.strip() for i in v.split(",")]
    elif isinstance(v, list | str):
        return v
    raise ValueError(v)

class Settings(BaseSettings):
    model_config = SettingsConfigDict(
        # Use top level .env file (one level above ./hunter-ai/)
        env_file=".env",
        env_ignore_empty=True,
        extra="ignore",
    )

    PROJECT_NAME: str = "Hunter AI"
    API_V1_STR: str = "/api/v1"
    SECRET_KEY: str = secrets.token_urlsafe(32)
    # 60 minutes * 24 hours * 7 days = 7 days
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 7
    FRONTEND_HOST: str = "http://localhost:5173"
    DATABASE_URL: str = "mysql+aiomysql://root:Wfn3QMe&5t@localhost:3306/hunter_ai"
    REDIS_URL: str = "redis://127.0.0.1:6379"
    REDIS_PASSWORD: str = ""

    BACKEND_CORS_ORIGINS: Annotated[
        list[AnyUrl] | str, BeforeValidator(parse_cors)
    ] = []

    @computed_field  # type: ignore[prop-decorator]
    @property
    def all_cors_origins(self) -> list[str]:
        return [str(origin).rstrip("/") for origin in self.BACKEND_CORS_ORIGINS] + [
            self.FRONTEND_HOST
        ]

    # WaveUp相关配置
    # AES加密密钥
    WP_APP_KEY: str = "ak_8x9y2b7m4n6d3p5q1r0s"
    WP_APP_SECRET: str = "as_f4k9j8m2n7b3v1c6x8z5q0w9e2r4t6y7u"
    WP_API_URL: str = "https://open-gateway-test.waveup.art"
    WP_TOKEN_EXPIRE_DAYS: int = 7 # WaveUp Token 7天过期
    WP_REDIRECT_URI: str = "http://47.98.192.52"
    # WP_REDIRECT_URI: str = "http://47.98.192.52/index.html"
    # 接口
    WP_QUERY_PRODUCTS : str = "/open-api/product/product-list-query"
    WP_QUERY_PRODUCT_PAIR : str = "/open-api/product-pair/list-query"
    WP_QUERY_WALLET : str = "/open-api/wallet/user-balance-query"
    WP_PRODUCT_SWAP : str = "/open-api/product-swap/swap"
    WP_GET_ACCESS_TOKEN : str = "/open-api/auth/get-access-token"
    WP_GET_USER_INFO : str = "/open-api/auth/get-user-info"
    WP_QUERY_USER_ASSET_LIST : str = "/open-api/asset/user-asset-list-query"

    # XM相关配置
    XM_API_URL: str = "https://api.x-metash.cn"
    XM_MARKET_ARCHIVE_PAGE : str = "/h5/market/marketArchivePage"
    XM_ARCHIVE_GOODS : str = "/h5/goods/archiveGoods"
    XM_SEARCH_APP_NEW: str = "/h5/home/searchAppNew"
    XM_MY_WALLET: str = "/h5/balance/myWallet"
    XM_PREPAY: str = "/h5/order/prepay"
    XM_CAN_OPEN_TRAN: str = "/h5/canOpenTran"
    XM_AGREEMENT_DETAILED: str = "/h5/agreement/detailed"
    XM_CHECK_AMOUNT: str = "/h5/order/check/amount"
    XM_AGREEMENT_SIGN: str = "/h5/agreement/sign"
    XM_CHECK_PEND_ORDER: str = "/h5/order/checkPendOrder"
    XM_VERIFY_WALLET_HASH: str = "/h5/openapi/verify/walletHash"
    XM_UNIFIED_PAY: str = "/h5/order/unifiedPay"
    XM_ARCHIVE_MARKET: str = "/h5/archive/archiveMarket"

    # redis缓存配置
    NFT_DETAIL_EXPIRE: int = 60 * 30 # 30分钟过期

settings = Settings()  # type: ignore